// import axios from "axios";
import { Upload } from "@/api/interface/index";
import http from "@/api/index";
import { useGlobalStore } from "@/stores/modules/global";
import { computed } from "vue";

//自定义post请求
export function postRequest(url: string, data: any) {
  return http.post(url, data);
}

//自定义get请求
export function getRequest(url: string, data: any) {
  return http.get(url, data);
}

export function getDetails(url: string, id: String) {
  return http.get(url + "/get/" + id, {});
}

export function getByPath(url: string, id: String) {
  return http.get(url + "/" + id, {});
}

export function deleteOne(url: string, id: String) {
  return http.get(url + "/delete/" + id, {});
}

export function doDelete(url: string, data: any) {
  return http.post(url + "/delete", data);
}

export function doAdd(url: string, data: any) {
  return http.post(url + "/add", data);
}

export function doUpdate(url: string, data: any) {
  return http.post(url + "/update", data);
}

//自定义查询列表
export function getList(url: string, data: any) {
  return http.post(url + "/query", data);
}

//自定义查询列表
export function queryList(url: string, data: any) {
  return http.post(url, data);
}

export function exportFileUrl(url: string, data: any) {
  return http.post(url, data);
}

//附件上传
export function uploadUrl(url: string, data: any) {
  return http.post(url, data);
}

/**
 * @name 文件上传模块
 */
// 图片上传
export const uploadFile = (params: FormData) => {
  return http.post<Upload.ResFileUrl>(`/system/file/uploadFile`, params);
};

/**
 * @name 图片上传模块
 */
// 图片上传
export const uploadImg = (params: FormData) => {
  return http.post<Upload.ResFileUrl>(`/system/file/uploadImg`, params);
};

// 视频上传
export const uploadVideo = (params: FormData) => {
  return http.post<Upload.ResFileUrl>(`/file/upload/video`, params);
};

//下载文件
export function downloadFile(url: string, data: any) {
  return http.download(url, data);
}

//下载附件
export function downloadAttachment(attamentUuid: string) {
  return http.download(`/system/attachment/download/${attamentUuid}`, {});
}

//下载附件
export async function downloadBlob(url: string, params: any) {
  const tempName = "testExport";
  const fileType = ".doc";
  try {
    const res = await http.download(url, params);
    const blob = new Blob([res]);
    // 兼容 edge 不支持 createObjectURL 方法
    if ("msSaveOrOpenBlob" in navigator) return window.navigator.msSaveOrOpenBlob(blob, tempName + fileType);
    const blobUrl = window.URL.createObjectURL(blob);
    const exportFile = document.createElement("a");
    exportFile.style.display = "none";
    exportFile.download = `${tempName}${fileType}`;
    exportFile.href = blobUrl;
    document.body.appendChild(exportFile);
    exportFile.click();
    // 去除下载对 url 的影响
    document.body.removeChild(exportFile);
    window.URL.revokeObjectURL(blobUrl);
  } catch (error) {
    console.log(error);
  }
}

/**
 * @description 表格高度（页面高度除去标签栏、操作栏、分页剩下的高度）
 * @returns {number}
 */
export function backTableHeight() {
  const height = document.documentElement.clientHeight;
  const globalStore = useGlobalStore();
  const footer = computed(() => globalStore.footer);
  if (footer.value) {
    return height - 316; // return height - 90 - 96 - 130;
  } else {
    return height - 286; //footer高度为30
  }
}
/**
 *
 * @returns 获取屏幕高度
 */
export function getClientHeight() {
  return document.documentElement.clientHeight;
}

/**
 *
 * @returns 获取数据为空的url
 */
export function getDataEmptyImg() {
  return new URL("/data_empty.png", import.meta.url).href;
}

export function getImgUrl(imgUrl: string) {
  if (imgUrl.startsWith("http")) {
    return new URL(imgUrl, import.meta.url).href;
  } else {
    return new URL(import.meta.env.VITE_STATIC_URL + imgUrl, import.meta.url).href;
  }
}

//枚举数据本地缓存
export function getLocalStorage() {
  return JSON.parse(String(localStorage.getItem("localStorageEnumData")));
}

//排序
export function doSorted(val: any, data: any) {
  if (val.prop) {
    if (val.order == "descending") {
      data.sorts = `${val.prop}@desc`;
    } else {
      data.sorts = `${val.prop}@asc`;
    }
  }
}

//设置路由页面跳转的参数
export function setRouteParams(routerName: string, params: {}) {
  localStorage.setItem("localStorageE_" + routerName, JSON.stringify(params));
}

//获取路由页面跳转的参数
export function getRouteParams(routerName: string) {
  return JSON.parse(String(localStorage.getItem("localStorageE_" + routerName)));
}
